package com.hs.device.charger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.common.core.exception.ServiceException;
import com.hs.common.core.utils.StringUtils;
import com.hs.common.security.utils.SecurityUtils;
import com.hs.communication.common.entity.Charger;
import com.hs.communication.common.entity.UserCharger;
import com.hs.device.charger.entity.dto.UserChargerDto;
import com.hs.device.charger.mapper.ChargerMapper;
import com.hs.device.charger.mapper.UserChargerMapper;
import com.hs.device.charger.service.UserChargerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserChargerServiceImpl extends ServiceImpl<UserChargerMapper, UserCharger> implements UserChargerService {

    @Resource
    private UserChargerMapper userChargerMapper;

    @Resource
    private ChargerMapper chargerMapper;

    @Override
    public int bind(UserChargerDto userChargerDto) {
        long userId = SecurityUtils.getUserId();
        String chargerSn = userChargerDto.getChargerSn();
        if (StringUtils.isNull(chargerSn) || StringUtils.isEmpty(chargerSn)) {
            throw new ServiceException("SN码不能为空");
        }

        LambdaQueryWrapper<Charger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Charger::getSn, userChargerDto.getChargerSn());
        Charger result = chargerMapper.selectOne(queryWrapper);
        if (result == null) {
            throw new ServiceException("请输入正确的SN码");
        }

        LambdaQueryWrapper<UserCharger> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(UserCharger::getUserId, userId).eq(UserCharger::getChargerSn, userChargerDto.getChargerSn());
        UserCharger result1 = userChargerMapper.selectOne(queryWrapper1);
        if (result1 != null) {
            throw new ServiceException("该设备已绑定");
        }


        LambdaQueryWrapper<UserCharger> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(UserCharger::getChargerSn, userChargerDto.getChargerSn());
        List<UserCharger> list = userChargerMapper.selectList(queryWrapper2);
        if (list.size() > 1) {
            throw new ServiceException("该设备已被别的用户绑定");
        }

        //更新充电桩表中的名字和地址字段
        LambdaUpdateWrapper<Charger> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Charger::getSn, userChargerDto.getChargerSn());
        if (StringUtils.isNotEmpty(userChargerDto.getName())) {
            updateWrapper.set(Charger::getName, userChargerDto.getName());
        }
        if (StringUtils.isNotEmpty(userChargerDto.getAddress())) {
            updateWrapper.set(Charger::getAddress, userChargerDto.getAddress());
        }
        if (StringUtils.isNotEmpty(userChargerDto.getName()) || StringUtils.isNotEmpty(userChargerDto.getAddress())) {
            chargerMapper.update(null, updateWrapper);
        }
        UserCharger build = UserCharger.builder().userId(userId).chargerSn(chargerSn).build();
        return userChargerMapper.insert(build);
    }

    @Override
    public int update(UserChargerDto userChargerDto) {
        int res = 0;
        //更新充电桩表中的名字和地址字段
        LambdaUpdateWrapper<Charger> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Charger::getSn, userChargerDto.getChargerSn());
        if (StringUtils.isNotEmpty(userChargerDto.getName())) {
            updateWrapper.set(Charger::getName, userChargerDto.getName());
        }
        if (StringUtils.isNotEmpty(userChargerDto.getAddress())) {
            updateWrapper.set(Charger::getAddress, userChargerDto.getAddress());
        }
        if (StringUtils.isNotEmpty(userChargerDto.getName()) || StringUtils.isNotEmpty(userChargerDto.getAddress())) {
            res = chargerMapper.update(null, updateWrapper);
        }
        return res;
    }

    @Override
    public int unbind(UserChargerDto userChargerDto) {
        long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<UserCharger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCharger::getUserId,userId)
                .eq(UserCharger::getChargerSn, userChargerDto.getChargerSn()) ;

        return userChargerMapper.delete(queryWrapper);
    }
}
